package wpwork.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wpwork.base.Pagination;
import wpwork.base.service.SuperServiceImpl;
import wpwork.entity.WorkResourcesEntity;
import wpwork.entity.WorkResourcesEntity;
import wpwork.mapper.ContractMapper;
import wpwork.mapper.WorkResourcesMapper;
import wpwork.service.ContractService;
import wpwork.service.WorkResourcesService;
import wpwork.util.RandomUtil;
import wpwork.util.StringUtil;
import wpwork.util.UserProvider;

import java.util.Collections;
import java.util.List;

/**
 *
 * @author wpwork开发平台组
 * @version V3.1.0
 * @copyright 武汉维普科技有限公司
 * @date 2021/3/16 9:47
 */
@Service
public class WorkResourcesServiceImpl extends SuperServiceImpl<WorkResourcesMapper, WorkResourcesEntity> implements WorkResourcesService {



    @Override
    public List<WorkResourcesEntity> getlist(int currentPage, int pageSize, String name, String type, boolean attendance, boolean inspection, boolean fence, boolean grid) {
        //通过UserProvider获取用户信息
        QueryWrapper<WorkResourcesEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtil.isEmpty(name)) {
            queryWrapper.lambda().and(
                    t -> t.like(WorkResourcesEntity::getName, name)
            );
        }
        if (!StringUtil.isEmpty(type)) {
            queryWrapper.lambda().and(
                    t -> t.like(WorkResourcesEntity::getType, type)
            );
        }
        if(attendance){
            queryWrapper.lambda().eq(WorkResourcesEntity::getAttendance, 1);
        }
        if(inspection){
            queryWrapper.lambda().eq(WorkResourcesEntity::getInspection, 1);
        }
        if(fence){
            queryWrapper.lambda().eq(WorkResourcesEntity::getFence, 1);
        }
        if(grid){
            queryWrapper.lambda().eq(WorkResourcesEntity::getGrid, 1);
        }
        //排序
        queryWrapper.lambda().orderByDesc(WorkResourcesEntity::getCreatorTime);
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(pageSize);
        Page<WorkResourcesEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<WorkResourcesEntity> userPage = this.page(page, queryWrapper);
        return pagination.setData(userPage.getRecords(), page.getTotal());
    }
}
